#!/usr/bin/env python
# -*- encoding: utf-8 -*-
import MySQLdb as mdb
import logging


class Order:
    def __init__(self, db_host, db_user_name, db_password, db_name):
        self._con = mdb.connect(db_host, db_user_name, db_password, db_name)
        self._con.autocommit(True);
        logging.info("[Order] - DB Init!")

        cur = self._con.cursor()
        cur.execute("SELECT VERSION()")
        ver = cur.fetchone()
        logging.info("[Order] - Database version : %s " % ver)

    def find_all_order(self, order='DESC'):
        cur = self._con.cursor()
        cur.execute("SELECT * FROM t_orders t ORDER BY t.`id` %s" % order)

        rows = cur.fetchall()

        # print "account amount blz_coin status charge_time delivered_time"
        #
        # for row in rows:
        #     print "%s %s %s %s %s %s" % (str(row[0]),str(row[1]),str(row[2]),str(row[3]),str(row[4]),str(row[5]))

        return rows

    def find_newest_order(self):
        cur = self._con.cursor()
        cur.execute("SELECT * FROM t_orders t ORDER BY t.`id` DESC LIMIT 1")
        row = cur.fetchone()
        return row

    def close(self):
        self._con.close()

if __name__ == '__main__':
    order = Order("180.97.187.89", "test", "1z2x3c4v", "313_auth")

    row = order.find_newest_order()
    print row

    order.close()
